package com.lagou.edu.controller;

import com.lagou.edu.pojo.Article;
import com.lagou.edu.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Calendar;

/**
 * @author xsl
 * @date 2020/8/16 18:37
 * @Description: MainController
 */
@Controller
public class MainController {

    @Autowired
    private ArticleService articleService;

    @RequestMapping("/articles/page")
    public String index(Model model, @RequestParam("pageNum") String pageNum) {
        int pagenum = 1;
        if(!StringUtils.isEmpty(pageNum)){
            pagenum = Integer.valueOf(pageNum);
        }
        Page<Article> all = articleService.findAll(PageRequest.of(pagenum, 2));
        model.addAttribute("articles", all.getContent());
        model.addAttribute("pageNum",pagenum);
        model.addAttribute("totalPages",all.getTotalPages());
        model.addAttribute("totalElements",all.getTotalElements());
        return "client/index";
    }

    @RequestMapping("/toLoginPage")
    public String toLoginPage(Model model) {
        model.addAttribute("currentYear", Calendar.getInstance().get(Calendar.YEAR));
        return "login";
    }
}
